﻿#include "IM_Gate.h"
#include "Base_MessageQueue.h"
#include "Base_Context.h"
#include "Base_MessageCore.h"
#include "IM_Socket_Server.h"
#include "IM_MessageDef.h"
#include "IM_Agent.h"
#include "Log.h"


IM_Gate::IM_Gate()
{
	m_header_size = 2;
	m_listen_socket_id = -1;
	m_contxt = nullptr;
	m_socket_server = nullptr;
}


IM_Gate::~IM_Gate()
{
}

bool IM_Gate::init(Base_MessageCore * base_msg_core, IM_Socket_Server *socket_server)
{
	m_msg_core = base_msg_core;
	m_contxt = Base_Context::new_base_context();
	Base_MessageCore::register_context(m_contxt);
	m_contxt->callback = [&](Base_Message * m) {this->_proc(m); };
	m_socket_server = socket_server;

	m_listen_socket_id = socket_server->sokcet_server_listen(m_contxt->get_handle(), "0.0.0.0", 8010, 128);

	return true;
}

int IM_Gate::socket_send(int socket_id,const void * buf, int len)
{
	uint8_t heard[2];
	Utility::ilittle_to_big_endian_16(heard,(uint16_t) len);

	DEBUG("发送头部\n");
	m_socket_server->socket_server_send(socket_id,(char *)heard, 2);

	if (len==0)
	{
		return 0;
	}
	DEBUG("发送数据部分\n");
	m_socket_server->socket_server_send(socket_id,(char *)buf, len);
	return 0;
}

void IM_Gate::_proc(Base_Message * b_msg)
{
	
	DEBUG("IM_Gate : _proc \n");

	switch (b_msg->type)
	{
		case IM_MessageType_Text:
			break;

		case IM_MessageType_Socket:
			//@sz   实际数据的大小   
			_dispatch_socket_message((IM_SocketMessage *)b_msg->data, b_msg->size-sizeof(IM_SocketMessage));
			break;

		case IM_MessageType_Client:

			break;
		default:
			break;
	}
}

void IM_Gate::_dispatch_socket_message(IM_SocketMessage * sokcet_msg, int sz)
{
	switch (sokcet_msg->type)
	{
		case SOCKET_ACCEPT:
			DEBUG("收到连接 socket_id : %d\n",sokcet_msg->ud);
			_accept_new_connection(sokcet_msg->ud);

			break;
		case SOCKET_DATA:
			DEBUG("收到消息 \n");

			/*DEBUG("msg:%s\n", sm->data);*/
			auto f=m_socketid_map_agent.find(sokcet_msg->socket_id);
			if (f!=m_socketid_map_agent.end())
			{
				DEBUG("找到对应的连接\n");
				_dispatch_message(f->second, sokcet_msg->socket_id, sokcet_msg->buffer, sokcet_msg->ud);
			}
			else
			{
				DEBUG("未找到对应的链接\n");

				/************************************************************************/
				/*                                                                      */
				/************************************************************************/

			}
			break;
	}
}

void IM_Gate::_dispatch_message(connection * c, int id, void * data, int size)
{
	DEBUG("分发数据\n");
	c->buffer.push_data((char* )data, size);
	while (true)
	{
		int size = c->buffer.read_header(m_header_size);
		DEBUG("数据大小：%d\n",size);
		if (size<0)
		{
			return;
		}
		else if(size>0)
		{
			if (size>=0x1000000)
			{

			}
			else
			{
				_forward(c, size);
				c->buffer.reset_databuffer();
			}

		}
	}
}

void IM_Gate::_forward(connection * c, int size)
{
	if (c->agent)
	{
		DEBUG("转发给agent  \n");
		char *temp = (char *)malloc(size);
		c->buffer.read_data(temp, size);
		assert(m_msg_core&&"m_msg_core 为NULL \n");
		m_msg_core->send_msg(IM_MessageType_Socket, m_contxt->get_handle(), c->agent, temp, size);
		DEBUG("消息已经投递给agent\n");
	}
}

void IM_Gate::_accept_new_connection(int socket_id)
{
	IM_Agent * agent=IM_Agent::create_agent(m_socket_server, socket_id,this);
	connection * c = new connection();
	c->agent = agent->get_context_handle();
	c->client = -1;
	m_socketid_map_agent.emplace(std::make_pair(socket_id,c));
}



void IM_Gate::_ctl(void * msg, int size)
{
	/*
	Base_Context * ctx = m_contxt;
	char tmp[size + 1];
	memcpy(tmp, msg, size);
	tmp[size] = '\0';
	char * cmmand = tmp;
	if (size==0)
	{
		return;
	}
	int i;
	for (i=0;i<size;i++)
	{
		if (cmmand[i]==' ')
		{
			break;
		}
	}

	if (memcmp(cmmand,"forward",i)==0)
	{

	}*/
}
